home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 446_01 / DOC / SPLINES / EX / PROG3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-18  |  1.6 KB  |  46 lines

  1. // ********************** File: prog3.C ************************
  2. // A program which demonstrates diverse interpolation algorithms
  3. // inside class BSpline and class ParametricBSpline
  4. #include <BSpline.h>
  5. #include <ParametricBSpline.h>
  6. #include <splineOutput.h>
  7.  
  8. int main()
  9. {
  10.   Vec(real) x(7), f(7);   // the discrete data to be interpolated
  11.   for (int i = 1; i <= 7; i++) { x(i)=0.6*i+0.6; f(i)=x(i)*x(i); }
  12.  
  13.   // first: Create knot vector, spline space and call BSpline::interpolate
  14.   //        (interpolation type as given by the spline space)
  15.   KnotVec knots(5);
  16.   knots.fill(1,5);  knots.regulate(4); // 1 1 1 1  2 3 4  5 5 5 5 
  17.   SplineSpace space(knots,4);
  18.   BSpline bs1;
  19.   bs1.redim(space); 
  20.   bs1.interpolation(x,f);
  21.   s_o << "Calling function 'interpolation' directly:\n";
  22.   real y;
  23.   for (i = 1; i <= 10; i++) 
  24.     { y = 1.2 + (i-1)*0.1; s_o << oform("%3.1f %4.2f\n",y,bs1(y)); }
  25.   s_o << '\n';
  26.  
  27.   // second: invoke cubicInterpolation and linearInterpolation directly,
  28.   //         knot vectors and spline spaces are automatically computed  
  29.   BSpline bs2,bs3;
  30.   s_o << "Cubic interpolation with natural boundary condition.\n\n";
  31.   bs2.cubicInterpolation(x,f,"Natural");
  32.   s_o << "Linear interpolation.\n\n";
  33.   bs3.linearInterpolation(x,f);
  34.  
  35.   // third: the spline function is treated as a parametric curve
  36.   Mat(real) data(7,2); // parametric curve data (x_i,f_i), i=1,..,7
  37.   for (i = 1; i <= 7; i++) { data(i,1)=x(i); data(i,2)=f(i); }
  38.  
  39.   ParametricBSpline pbs;
  40.   s_o << "Parametric cubic interpolation.\n";
  41.   pbs.cubicInterpolation(data,"Free","Centripetal");
  42.  
  43.   createPlotmtvFile(pbs,1,"FILE=spline.mtv");  // plotmtv format to file
  44.   return 0;
  45. }
  46.